<?php

namespace Admin\Model;

use Think\Model;

class SortModel extends Model {

    protected $trueTableName = 'sort';
    protected $_validate = [
        ['class', 'require', '标题不能为空'],
    ];

    /** 递归获取子孙栏目
     * @param $cates
     */
    public function findSubTree($cates, $id = 0, $lev = 1) {
        $subtree = []; //子孙数组
        foreach ($cates as $v) {
            if ($v['pid'] == $id) {
                $v['lev'] = $lev;
                $subtree[] = $v;
                // var_dump($v);die;

                $subtree = array_merge($subtree, $this->findSubTree($cates, $v['id'], $lev + 1));
            }
        }
        return $subtree;
    }

    /**
     * 
     * 获得树状数据
     * @return [type] [description]
     */
    public function tree($data) {

        foreach ($data as $k => $v) {
            if ($v['lev'] >= 2) {
                $str = "";

                for ($i = 0; $i < $v['lev'] - 1; $i++) {
                    $str .= "&nbsp;&nbsp;&nbsp;&nbsp";
                }
            }
            if ($v['lev'] != 1) {
                if ($data[$k + 1]['lev'] > $data[$k]['lev']) {
                    $data[$k]['_html'] = $str . "├─" . $v['class'];
                } else {
                    $data[$k]['_html'] = $str . "├─" . $v['class'];
                }
            } else {
                $data[$k]['_html'] = $v['class'];
            }
        }
        return $data;
    }

    public function qaz()
    {
        echo 1;
    }
}

?>